from django.http import HttpResponse
from django.shortcuts import render_to_response
from handle import *
from forms import ItemForm


def budget(request,flag='0',itemid_idx='0_0'):
	ItemList=GetList()
	if str(flag)==str('1'):
		initItemid=itemid_idx.split('_')[0]
		return render_to_response('budget.html',{'itemlist':ItemList,'initItemid':initItemid})
	if str(flag)==str('2'):
		initItemid=itemid_idx.split('_')[0]
		initidx=itemid_idx.split('_')[1]
		DelItemInfo(initidx)
		return render_to_response('budget.html',{'itemlist':ItemList,'initItemid':initItemid})
	if str(flag)==str('3'):
		initItemid=itemid_idx.split('_')[0]
		initidx=itemid_idx.split('_')[1]
		realnum=request.GET['realnum']
		ModifyRGF(initidx,realnum)
		return render_to_response('budget.html',{'itemlist':ItemList,'initItemid':initItemid})
	if request.method == 'POST':
		RequestInfo=request.POST
		form=ItemForm(RequestInfo)
		if form.is_valid():
			OneItemInfo=form.cleaned_data
			initItemid=OneItemInfo['itemid']
			ModifyItemInfo(OneItemInfo)
		else:
			idx=RequestInfo['idx']
			itemid=RequestInfo['itemid']
			return render_to_response('itemchange.html',{'form': form,'idx':idx,'itemid':itemid})
	else:
		initItemid=0
	return render_to_response('budget.html',{'itemlist':ItemList,'initItemid':initItemid})
			
def budget_body(request,itemid):
	if str(itemid)==str(0):
		ItemInfo=GetItemInfo()
		return render_to_response('budget_body.html',{'iteminfo':ItemInfo})
	else:
		ItemName,ItemDetail=GetItemDetail(itemid)
		return render_to_response('budget_body2.html',{'itemDetail':ItemDetail,'itemid':itemid,'itemname':ItemName})
			
def itemchange(request,itemid_idx):
	idx=itemid_idx.split('_')[1]
	itemid=itemid_idx.split('_')[0]
	if str(idx)==str(0):
		form = ItemForm(initial={'num':1,'satisfaction':3})
	else:
		OneItemInfo=GetOneItmeInfo(idx)
		form = ItemForm(initial=OneItemInfo)
	return render_to_response('itemchange.html',{'form': form,'idx':idx,'itemid':itemid})

def picdata(request):
	PicInfo=GetPicInfo()
	return HttpResponse(PicInfo)
	
def rgf(request,itemid):
	rgf_info=GetRGF(itemid)
	return render_to_response('rgf.html',{'rgf_info':rgf_info,'itemid':itemid})
